package com.example.music_player_app;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.music_player_app.api.service.login.LoginService;
import com.example.music_player_app.utils.ApiServer;
import com.example.music_player_app.utils.DBHelper;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    View view;
    Toolbar toolbar; // 导航栏
    DrawerLayout drawerLayout;
    private SharedPreferences sp; // 基本配置
    private SharedPreferences.Editor ed;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化登录
        initLogin();

        view = findViewById(R.id.content_main);
        toolbar = view.findViewById(R.id.tool_bar);

        drawerLayout = findViewById(R.id.activity_main);
        setSupportActionBar(toolbar);

        BottomNavigationView navView = view.findViewById(R.id.bottom_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main2);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);



        // 基本配置,文件名base_settings.xml
        sp = getSharedPreferences("base_settings", Context.MODE_PRIVATE);
        ed = sp.edit();
        if(!sp.contains("isFirstInstall")) { // 判断是否是第一次安装
            initSettings(); // 初始化基本配置
        }
        ed.putBoolean("isPlaying", false); // 是否有音乐正在播放
        ed.apply();


        // 初始化SQLite数据库
        initDataBases();
    }

    private void initDataBases() {
        DBHelper dbHelper = new DBHelper(MainActivity.this, "musicPlayerDB.db", null, 1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "create table if not exists myMusic(id Integer primary key, name text, singer text, songImgUrl text, playUrl text )";
        db.execSQL(sql);
    }

    @SuppressLint("CommitPrefEdits")
    private void initSettings() {
        ed.putBoolean("isFirstInstall", true); // 设置第一次安装为true
    }

    private void initLogin() {
        new Thread(){
            @Override
            public void run() {
                ApiServer http = new ApiServer();
                LoginService loginService = (LoginService) http.getRetrofit().create(LoginService.class);
                Call<ResponseBody> call = loginService.post(
                        "13665149901",
                        "zhuqingyao");
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            Log.i("run", "onLogin: " + response.body().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Toast.makeText(MainActivity.this,"数据初始化失败，请检查网络连接。"
                                ,Toast.LENGTH_LONG).show();
                    }
                });
            }
        }.start();
    }


}